package com.yunxi.controller.sat;

import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.CurrentUser;
import com.yunxi.core.web.LoginInfo;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.sat.pojo.SatCustomerAssignClueVO;
import com.yunxi.sat.pojo.SatCustomerClueVO;
import com.yunxi.sat.service.SatCustomerClueDubboService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * <p>
 * 客户线索 前端控制器
 * </p>
 *
 * @author caoYong
 * @since 2019-07-10
 */
@RestController
@RequestMapping("/admin/satCustomerClue")
@Api(tags = "sat:customerClue", description = "客户线索接口")
public class SatCustomerClueController {
    private static final Logger logger = LoggerFactory.getLogger(SatCustomerClueController.class);

    @Autowired
    SatCustomerClueDubboService satCustomerClueDubboService;

    /**
     * 线索查询接口
     *   大区经理查询 该大区经理所配置的大区下的线索
     *   小区经理查询 该小区经理所配置的城市下的线索
     * @param satCustomerClueVO
     * @param pageNum
     * @param pageSize
     * @param loginInfo
     * @return
     * @throws Exception
     */
    @GetMapping("/page")
    @ApiOperation(value = "查询分页")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "pageNum", value = "分页页码", required = true, dataType = "int"),
            @ApiImplicitParam(paramType="query", name = "pageSize", value = "分页数量", required = true, dataType = "int")
    })
    public ApiResult page(SatCustomerClueVO satCustomerClueVO, Integer pageNum, Integer pageSize,@CurrentUser LoginInfo loginInfo) throws Exception {
        satCustomerClueVO.setPartnerCode(loginInfo.getPartnerCode());
        return ResponseUtil.ok(satCustomerClueDubboService.findPageByVO(satCustomerClueVO, pageNum, pageSize,loginInfo));
    }

    @GetMapping("/pagePartner")
    @ApiOperation(value = "查询分页")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "pageNum", value = "分页页码", required = true, dataType = "int"),
            @ApiImplicitParam(paramType="query", name = "pageSize", value = "分页数量", required = true, dataType = "int")
    })
    public ApiResult pagePartner(SatCustomerClueVO satCustomerClueVO, Integer pageNum, Integer pageSize,@CurrentUser LoginInfo loginInfo) throws Exception {
        satCustomerClueVO.setPartnerCode(loginInfo.getPartnerCode());
        return ResponseUtil.ok(satCustomerClueDubboService.findPageByVO(satCustomerClueVO, pageNum, pageSize,loginInfo));
    }

    @GetMapping("/load")
    @ApiOperation(value = "读取单个")
    @ApiImplicitParam(paramType="query", name = "clueId", value = "主键id", required = true, dataType = "String")
    public ApiResult load(String clueId, @CurrentUser LoginInfo loginInfo) throws Exception {
        return ResponseUtil.ok(satCustomerClueDubboService.getById(clueId));
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存信息")
    public ApiResult save(@RequestBody SatCustomerClueVO satCustomerClueVO, @CurrentUser LoginInfo loginInfo) throws Exception {
        satCustomerClueDubboService.saveOrUpdate(satCustomerClueVO);
        return ResponseUtil.ok("success");
    }

    @PostMapping("/getClue")
    @ApiOperation(value = "领取线索")
    public ApiResult getClue(@RequestBody SatCustomerClueVO satCustomerClueVO, @CurrentUser LoginInfo loginInfo) throws Exception {
        satCustomerClueDubboService.getClue(satCustomerClueVO.getClueId(),loginInfo);
        return ResponseUtil.ok("success");
    }

    @PostMapping("/assignClueToS")
    @ApiOperation(value = "分配线索给销售顾问")
    public ApiResult assignClue(@RequestBody SatCustomerClueVO satCustomerClueVO, @CurrentUser LoginInfo loginInfo) throws Exception {
        satCustomerClueDubboService.assignClue(satCustomerClueVO.getClueId(),satCustomerClueVO.getGetUser(),loginInfo);
        return ResponseUtil.ok("success");
    }

    @PostMapping("/assignClueToP")
    @ApiOperation(value = "分配线索给渠道商")
    public ApiResult assignClueToP(@RequestBody SatCustomerClueVO satCustomerClueVO, @CurrentUser LoginInfo loginInfo) throws Exception {
        satCustomerClueDubboService.assignClueToP(satCustomerClueVO.getClueId(),satCustomerClueVO.getPartnerCode(),loginInfo);
        return ResponseUtil.ok("success");
    }

    @GetMapping("/todayClueCount")
    @ApiOperation(value = "今日待分配线索数")
    public ApiResult todayClueCount(@CurrentUser LoginInfo loginInfo) throws Exception {
        return ResponseUtil.ok(satCustomerClueDubboService.todayClueCount(loginInfo));
    }

    @PostMapping("/areaManager")
    @ApiOperation(value = "大区经理分配线索")
    public ApiResult areaManager(@RequestBody SatCustomerAssignClueVO satCustomerAssignClueVO, @CurrentUser LoginInfo loginInfo) throws Exception {
        satCustomerClueDubboService.areaManager(satCustomerAssignClueVO.getList(),satCustomerAssignClueVO.getPartnerCode(),loginInfo.getUserCode(),loginInfo);
        return ResponseUtil.ok("success");
    }

    @PostMapping("/autoAssign")
    @ApiOperation(value = "自动分配线索")
    public ApiResult autoAssign(@RequestBody List<SatCustomerClueVO> list, @CurrentUser LoginInfo loginInfo) throws Exception {
        satCustomerClueDubboService.autoAssign(list,loginInfo);
        return ResponseUtil.ok("success");
    }
}
